package org.edu.prj.langton.lefebvre.calcul;

import org.edu.prj.langton.lefebvre.impl.Fourmi;
import org.edu.prj.langton.lefebvre.impl.Fourmi.Direction;

public class Logique {

	public static void move (Fourmi fourmi) 
	{
//		System.out.println("MOVE");
//		System.out.println(fourmi);
//		System.out.println("cell = " + fourmi.getM_lMatrix().getValue(fourmi.getM_x(), fourmi.getM_y()));
		// 4 possibility with the direction

		switch(fourmi.getM_direction())
		{
		case NORD:
			if(fourmi.getM_lMatrix().getValue(fourmi.getM_x(), fourmi.getM_y()))
			{
				//white
				moveToOuest(fourmi);
			}
			else
			{
				//black
				moveToEst(fourmi);
			}
			break;
		case SUD:
			if(fourmi.getM_lMatrix().getValue(fourmi.getM_x(), fourmi.getM_y()))
			{
				//white
				moveToEst(fourmi);
			}
			else
			{
				//black
				moveToOuest(fourmi);
			}
			break;
		case OUEST:
			if(fourmi.getM_lMatrix().getValue(fourmi.getM_x(), fourmi.getM_y()))
			{
				//white
				moveToSud(fourmi);	
			}
			else
			{
				//black
				moveToNord(fourmi);
			}
			break;
		case EST:
			if(fourmi.getM_lMatrix().getValue(fourmi.getM_x(), fourmi.getM_y()))
			{
				//white
				moveToNord(fourmi);
			}
			else
			{
				//black
				moveToSud(fourmi);
			}
			break;
		}
		
		changeState(fourmi);

//		throw new RuntimeException("new direction not found");

	}

	private static void moveToOuest(Fourmi fourmi)
	{
//		System.out.println("moveToOuest");
		fourmi.setM_direction(Direction.OUEST);
		if(fourmi.getM_x() == 0)
		{
			fourmi.setM_x(fourmi.getM_lMatrix().getNumberOfColumn() - 1);
		}
		else
		{
			fourmi.setM_x(fourmi.getM_x() - 1);
		}
//		System.out.println(fourmi);
	}
	private static void moveToEst(Fourmi fourmi)
	{
//		System.out.println("moveToEst");
		fourmi.setM_direction(Direction.EST);

		if(fourmi.getM_x() == (fourmi.getM_lMatrix().getNumberOfColumn() - 1))
		{
			fourmi.setM_x(0);
		}
		else
		{
			fourmi.setM_x(fourmi.getM_x() + 1);
		}
//		System.out.println(fourmi);
	}
	private static void moveToNord(Fourmi fourmi)
	{
//		System.out.println("moveToNord");

		fourmi.setM_direction(Direction.NORD);

		if(fourmi.getM_y() == 0)
		{
			fourmi.setM_y(fourmi.getM_lMatrix().getNumberOfLine() - 1);
		}
		else
		{
			fourmi.setM_y(fourmi.getM_y() - 1);
		}
		
//		System.out.println(fourmi);

	}
	private static void moveToSud(Fourmi fourmi)
	{
//		System.out.println("moveToSud");

		fourmi.setM_direction(Direction.SUD);

		if(fourmi.getM_y() == fourmi.getM_lMatrix().getNumberOfLine() - 1)
		{
			fourmi.setM_y(0);
		}
		else
		{
			fourmi.setM_y(fourmi.getM_y() + 1);
		}
//		System.out.println(fourmi);

	}
	
	private static void changeState(Fourmi fourmi)
	{
//		System.out.println("changeState");
		//change state of new cell
		if(fourmi.getM_lMatrix().getValue(fourmi.getM_x(), fourmi.getM_y()))
		{
//			System.out.println("New cell is active");
			fourmi.getM_lMatrix().unActivateCell(fourmi.getM_x(), fourmi.getM_y());
		}
		else
		{
//			System.out.println("New cell is unactive");
			fourmi.getM_lMatrix().activateCell(fourmi.getM_x(), fourmi.getM_y());
		}

	}

}
